package com.android.aiod.manager;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.util.GlideUtil;
import com.android.aiod.bean.MediaInfo;
import com.android.aiod.ui.view.MyJzvdStd;
import com.blankj.utilcode.util.FileUtils;
import com.wangpm.enteranimation.EnterAnimLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JzvdStd;

public class MediaPlayManager {

    Context mContext;
    AnimManager animManager;
    MyJzvdStd jz_video;
    ImageView iv_img;
    List<MediaInfo> mediaInfos;
    public int currentPlayIndex = -1;  //当前播放第几个
    Handler handler = new Handler();
    boolean isFirst = true;  //第一张图片不放动画

    public MediaPlayManager(Context mContext, MyJzvdStd jz_video, ImageView iv_img, EnterAnimLayout anim_view) {
        this.jz_video = jz_video;
        this.mContext = mContext;
        this.iv_img = iv_img;
        animManager = new AnimManager(mContext, anim_view);
        jz_video.setCallBack(callBack);
        getFile();

        if (mediaInfos.size() != 0) {
            playNext();
        }
    }

    CommCallBack callBack = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            if(mediaInfos.size()==1)  //只有一张图片结束切换
            {
                return;
            }
            playNext();
        }
    };

    public void getFile() {
        mediaInfos = new ArrayList<>();
        String path = Environment.getExternalStorageDirectory() + "/AIOD/Media";
        Log.i("path:" + path);
        List<File> list = FileUtils.listFilesInDir(path);

        for (File file : list) {
            if (file.isFile()) {
                Log.i("file:" + file.getName());
                MediaInfo mediaInfo = new MediaInfo();
                mediaInfo.path = file.getPath();
                if (isImg(file.getPath().toLowerCase())) {
                    //是图片
                    mediaInfo.type = 0;
                    mediaInfos.add(mediaInfo);
                } else if (isVideo(file.getPath().toLowerCase())) {
                    mediaInfo.type = 1;
                    mediaInfos.add(mediaInfo);
                }
            }
        }
    }

    private void playNext() {

        if (currentPlayIndex == mediaInfos.size() - 1) currentPlayIndex = -1;
        currentPlayIndex++;
        MediaInfo mediaInfo = mediaInfos.get(currentPlayIndex);
        switch (mediaInfo.type) {
            case 0:   //图片
                play_img(mediaInfo);
                break;
            case 1:  //视频
                play_video(mediaInfo);
                break;
        }
        if (!isFirst) {
            animManager.anim();
        } else {
            isFirst = false;
        }
    }

    //播放图片
    private void play_img(MediaInfo mediaInfo) {
        jz_video.setVisibility(View.GONE);
//        Log.i("play_img:" + mediaInfo.path);
        GlideUtil.displayImage(mContext, mediaInfo.path, iv_img, -1);
        if(mediaInfos.size()==1)  //只有一张图片结束切换
        {
            return;
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                playNext();
            }
        }, ConfigManager.instance().configInfo.media_img_show_time* 1000);
    }

    //播放视频
    private void play_video(MediaInfo mediaInfo) {
        jz_video.setVisibility(View.VISIBLE);
        jz_video.setUp(mediaInfo.path, "", JzvdStd.SCREEN_NORMAL);
        jz_video.startVideo();

    }

    private boolean isVideo(String file) {
        if (file.endsWith("mp4") || file.endsWith("avi") || file.endsWith("wmv") || file.endsWith("ts") || file.endsWith("webp")) {
            return true;
        }
        return false;
    }

    private boolean isImg(String file) {
        if (file.endsWith("png") || file.endsWith("jpg") || file.endsWith("gif") || file.endsWith("jpeg")) {
            return true;
        }
        return false;
    }
}
